﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
    @Styles.Render("~/layui/css")

</head>
<body>
    <table id="demo" lay-filter="test"></table>



    @Scripts.Render("~/bundles/jquery")
    @Scripts.Render("~/bundles/layui")
    <script>
        var tableIns;
        layui.use(['table'], function () {
            var table = layui.table;

            //第一个实例
            tableIns = table.render({
                elem: '#demo'
                /*, height: 312*/
                , url: '/System/Users/GetUsersListPage' //数据接口
                , toolbar: '#mytoolbar'
                , page: true //开启分页
                , cols: [[ //表头
                    { type: "checkbox", fixed: 'left' }
                    , { field: 'userID', title: '编号', width: 100, sort: true }
                    , { field: 'path', title: '头像', templet: "#myimg", width: 60 }
                    , { field: 'loginName', title: '账户' }
                    , { field: 'name', title: '姓名', sort: true }
                    , { field: 'sex', title: '性别', width: 80 }
                    , { field: 'phone', title: '手机', width: 120, }
                    , { field: 'roleName', title: '角色', width: 100, }
                    , { field: 'status', title: '状态', width: 80, templet: "#mystatus" }
                    , { field: 'lastTime', title: '时间', width: 160, templet: "#mydatetime" }
                    , { title: '操作', width: 180, templet: "#mytool" }

                ]]
                , where: { name: $("#edtSearch").val() }
                , done(data) {
                    console.log(data);
                }
            });

            //触发事件
            table.on('toolbar(test)', function (obj) {
                var checkStatus = table.checkStatus(obj.config.id);
                switch (obj.event) {
                    case 'add':
                        parent.layer.open({
                            type: 2,
                            title: "添加",
                            area: ["500px", "560px"],
                            content: "/System/Users/Create"
                        })

                        break;
                    case 'delete':
                        if (checkStatus.data.length < 1) {
                            parent.layer.msg("请选择需要删除的项！", { icon: 0 });
                        } else {
                            var ids = [], names = "";
                            $(checkStatus.data).each(function (i, d) {
                                ids.push(d.userID);
                                names += names.length > 0 ? "," + d.name : d.name;
                            })
                            parent.layer.confirm('确定删除【' + names + '】', { icon: 3, title: '提示' }, function (index) {
                                $.post("/System/Users/DeleteBatch", { ids: ids }, function (data) {
                                    if (data.code > 0) {
                                        layer.msg(data.msg, { icon: 6 });
                                        tableIns.reload({
                                            where: { //设定异步数据接口的额外参数，任意设
                                                name: $("#edtSearch").val()
                                            }
                                            , page: {
                                                curr: 1 //重新从第 1 页开始
                                            }
                                        });

                                    } else {
                                        parent.layer.msg(data.msg, { icon: 5 })
                                    }
                                })
                                parent.layer.close(index);
                            });
                        }
                        break;
                    case 'update':
                        if (checkStatus.data.length < 1) {
                            layer.msg("请选择需要修改的项！", { icon: 0 });
                        } else if (checkStatus.data.length > 1) {
                            layer.msg("一次只能修改一项！", { icon: 0 });
                        } else {
                            parent.layer.open({
                                type: 2,
                                title: "修改",
                                area: ["500px", "460px"],
                                content: "/System/Users/Edit/" + checkStatus.data[0].userID
                            })
                        }
                        break;
                    case 'search':
                        tableIns.reload({
                            where: { //设定异步数据接口的额外参数，任意设
                                name: $("#edtSearch").val()
                            }
                            , page: {
                                curr: 1 //重新从第 1 页开始
                            }
                        });
                        break;
                };
            });

            //工具条事件
            table.on('tool(test)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
                var data = obj.data; //获得当前行数据
                var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

                switch (layEvent) {
                    case "detail":
                        parent.layer.msg("详情");
                        break;
                    case "del":
                        parent.layer.confirm('确定删除【' + data.name + '】', { icon: 3, title: '提示' }, function (index) {
                            $.post("/System/Users/Delete", { id: data.userID }, function (data) {
                                if (data.code > 0) {
                                    parent.layer.msg(data.msg, { icon: 6 });
                                    tableIns.reload({
                                        where: { //设定异步数据接口的额外参数，任意设
                                            name: $("#edtSearch").val()
                                        }
                                        , page: {
                                            curr: 1 //重新从第 1 页开始
                                        }
                                    });

                                } else {
                                    parent.layer.msg(data.msg, { icon: 5 })
                                }
                            })
                            layer.close(index);
                        });
                        break;
                    case "edit":
                        parent.layer.open({
                            type: 2,
                            title: "修改",
                            area: ["500px", "460px"],
                            content: "/System/Users/Edit/" + data.userID
                        })
                        break;
                    default:
                }
            });
        });
    </script>

</body>
</html>


@* 头部工具条 *@
<script type="text/template" id="mytoolbar">
    <div class="layui-table-tool-temp">
        <div class="layui-inline" lay-event="add">
            <i class="layui-icon layui-icon-add-1"></i>
        </div>
        <div class="layui-inline" lay-event="update">
            <i class="layui-icon layui-icon-edit"></i>
        </div>
        <div class="layui-inline" lay-event="delete">
            <i class="layui-icon layui-icon-delete"></i>
        </div>
        <div class="layui-inline" style="width:220px;">
            <div class="input-group">
                <input type="text" id="edtSearch" class="layui-input input-sm" placeholder="请输入姓名关键字">
                <div class="input-group-btn" lay-event="search">
                    <button class="layui-btn layui-btn-sm layui-btn-primary icon">
                        <i class="layui-icon layui-icon-search"></i>
                    </button>
                </div>
            </div>
        </div>
    </div>
</script>
@* 列工具条 *@
<script type="text/template" id="mytool">
    <a class="layui-btn layui-btn-xs" lay-event="detail">查看</a>
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
@* 处理状态标签 *@
<script type="text/template" id="mystatus">
    {{# if(d.status==true){ }}
    <span class="layui-badge layui-bg-green">正常</span>
    {{# } }}
    {{# if(d.status==false){ }}
    <span class="layui-badge">冻结</span>
    {{# } }}
</script>
@* 处理时间模板 *@
<script type="text/template" id="mydatetime">
    {{ d.lastTime.replace("T", " ").substr(0,19) }}
</script>
@* 处理头像模板 *@
<script type="text/template" id="myimg">
    <img class="layui-tb-img" src="{{d.path}}" />
</script>
